Skip to content

Build Soup.texts without @ (List.append)#64

Open
tysg wants to merge 1 commit intoaantron:masterfrom
ahrefs:perf/avoid-list-concat
Open

Build Soup.texts without @ (List.append)#64
tysg wants to merge 1 commit intoaantron:masterfrom
ahrefs:perf/avoid-list-concat

Conversation

@tysg
Copy link
Copy Markdown

@tysg tysg commented Dec 15, 2025

Thank you for this library!
During our internal testing we found an allocation hotspot in Soup.texts. The current implementation is equivalent to running List.append on singleton lists, i.e.

((([s] @ [s]) @ [s]) @ [s]) @ [s] ...

This PR changed Soup.texts to use List.cons and turn the implementation into

s :: s :: s :: ...

This avoids

  • allocating the intermediate list (i.e. [s]) then unwrap it
  • creating a copy of accumulator list for every List.append in List.fold_left

@tysg tysg changed the title Build texts without List.concat Build Soup.texts without @ (List.concat) Dec 15, 2025
@tysg tysg changed the title Build Soup.texts without @ (List.concat) Build Soup.texts without @ (List.append) Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant